/**
 * JAVACC DEMO 1.0
 * @copy right apache company All rights reserved. 
 * @Package com.apache.rpc.service.impl.helper  
 */
package com.apache.rpc.service.impl.helper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jxl.common.Logger;

import com.apache.database.constant.SystemTools;
import com.apache.database.constant.Validator;
import com.apache.database.db.IDao;
import com.apache.rpc.entity.InterfaceRegister;

/**
 * description:  接口注删除缓存策略
 * @author Hou Dayu 创建时间：2014-10-31  
 * @Copyright(c)2014:
 */
public class CacheTactics {

	private IDao interfaceRegisterDao;
	private Logger log = Logger.getLogger(CacheTactics.class);

	public void setInterfaceRegisterDao(IDao interfaceRegisterDao) {
		this.interfaceRegisterDao = interfaceRegisterDao;
	}

	public void initCaches() {
		try {
			String flag = String.valueOf(SystemTools.getInstance().getCache("")
					.getCacheObjectByKey("interfaceRegisterList"));
			if ("true".equals(flag))
				return;
			interfaceRegisterDao.initSystemCache();
		} catch (Exception e) {
			log.error("初始化接口缓存失败", e);
		}
	}

	/**
	 * description:  添加接口注册缓存
	 * @param register   
	 * @author Hou Dayu  
	 * @update 2014-10-31
	 */
	public void addInterfaceRegisterCache(InterfaceRegister register) {

		List<String> list = selectInterfaceRegisterCache(register.getBeanId());
		if (Validator.isEmpty(list)) {
			list = new ArrayList<String>();
		}
		list.add(register.getRegisterId());
		SystemTools.getInstance().getCache("").createCacheObject(cacheKey(register.getBeanId()), list);
	}

	/**
	 * description: 修改接口注册缓存 
	 * @param register   
	 * @author Hou Dayu  
	 * @update 2014-10-31
	 */
	public void editInterfaceRegisterCache(InterfaceRegister register) {
		InterfaceRegister reg = (InterfaceRegister) SystemTools.getInstance().getCache("")
				.getCacheObjectByKey(cacheKey(register.getRegisterId()));
		List<String> ids = selectInterfaceRegisterCache(reg.getBeanId());
		if (!Validator.isEmpty(ids)) {
			ids.remove(reg.getRegisterId());
			SystemTools.getInstance().getCache("").createCacheObject(cacheKey(reg.getBeanId()), ids);
		}
		List<String> list2 = selectInterfaceRegisterCache(reg.getBeanId());
		if (Validator.isEmpty(list2)) {
			list2 = new ArrayList<String>();
		}
		list2.add(register.getRegisterId());
		SystemTools.getInstance().getCache("").createCacheObject(cacheKey(register.getBeanId()), list2);
	}

	/**
	 * description:  删除接口注册缓存
	 * @param id
	 * @param beanId   
	 * @author Hou Dayu  
	 * @update 2014-10-31
	 */
	public void delInterfaceRegisterCache(String id, String beanId) {
		List<String> ids = selectInterfaceRegisterCache(beanId);
		if (Validator.isEmpty(ids))
			return;

		ids.remove(id);
		SystemTools.getInstance().getCache("").createCacheObject(cacheKey(beanId), ids);
	}

	/**
	 * description:  按BeanId获取接口注册可用的信息
	 * @param beanId
	 * @param flag
	 * @param methodName
	 * @return   
	 * @author Hou Dayu  
	 * @update 2014-10-31
	 */
	@SuppressWarnings("rawtypes")
	public Map<String, List<InterfaceRegister>> selectInterfaceRegisterCache(String beanId, String flag,
			String methodName) {
		List<String> ids = selectInterfaceRegisterCache(beanId);
		List list = (List) SystemTools.getInstance().getCache("").getObjects(ids, "interfaceRegister_");
		if (Validator.isEmpty(list))
			return null;
		Map<String, List<InterfaceRegister>> resMap = new HashMap<String, List<InterfaceRegister>>();
		for (Object obj : list) {
			InterfaceRegister register = (InterfaceRegister) obj;
			if (flag.equals(register.getFlag()) && methodName.equals(register.getMethodName())) {
				List<InterfaceRegister> result = resMap.get(register.getCallType());
				if (Validator.isEmpty(result)) {
					result = new ArrayList<InterfaceRegister>();
				}
				result.add(register);
				resMap.put(register.getCallType(), result);
			}
		}
		return resMap;
	}

	/**
	 * description:  按BeanId获取接口注册所有信息
	 * @param beanId
	 * @return   
	 * @author Hou Dayu  
	 * @update 2014-10-31
	 */
	@SuppressWarnings("unchecked")
	public List<String> selectInterfaceRegisterCache(String beanId) {

		List<String> list = (List<String>) SystemTools.getInstance().getCache("").getCacheCloneByKey(cacheKey(beanId));
		return list;
	}

	private String cacheKey(String beanId) {
		return "interfaceRegister_" + beanId;
	}
}
